package com.ppc.controller.admin;

import com.ppc.constant.JwtClaimsConstant;
import com.ppc.dto.AdminLoginDTO;
import com.ppc.dto.ClaimSolveDTO;
import com.ppc.dto.ComplaintSolveDTO;
import com.ppc.entity.AdminPPC;
import com.ppc.properties.JwtProperties;
import com.ppc.result.PageResult;
import com.ppc.result.Result;
import com.ppc.service.AdminService;
import com.ppc.utils.JwtUtil;
import com.ppc.vo.AdminClaimDetailVO;
import com.ppc.vo.AdminComplaintDetailVO;
import com.ppc.vo.AdminLoginVO;
import com.ppc.vo.ClaimDetailVO;
import com.ppc.vo.ComplaintDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties.Admin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author dy
 * @date 2023/12/20
 */
@Slf4j
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员相关接口")
public class AdminController {

  @Autowired
  private JwtProperties jwtProperties;

  @Autowired
  private AdminService adminService;

  /**
   * @param adminPPC
   * @return {@link Result }
   * @description 添加管理员账户
   */
  @PostMapping("/register")
  @ApiOperation("添加管理员账户")
  public Result register(@RequestBody AdminPPC adminPPC) {
    log.info("管理员注册:{}", adminPPC);
    adminService.register(adminPPC);
    return Result.success();
  }


  /**
   * @param adminLoginDTO
   * @return {@link Result }<{@link AdminLoginVO }>
   * @description 管理员登录
   */
  @PostMapping("/login")
  @ApiOperation("管理员登录")
  public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO) {
    log.info("管理员登录：{}", adminLoginDTO);

    AdminLoginVO admin = adminService.login(adminLoginDTO);

    //登录成功后，生成jwt令牌
    Map<String, Object> claims = new HashMap<>();
    claims.put(JwtClaimsConstant.ADMIN_ID, admin.getAdminId());
    String token = JwtUtil.createJWT(
        jwtProperties.getAdminSecretKey(),
        jwtProperties.getAdminTtl(),
        claims);

    admin.setToken(token);

    log.info("查询到管理员，{}", admin);
    return Result.success(admin);
  }

  /**
   * @return {@link Result }
   * @description 管理员退出登录
   */
  @GetMapping("/logout")
  @ApiOperation("管理员退出")
  public Result logout() {
    log.info("管理员退出");
    return Result.success();
  }

  /**
   * @param claimSolveDTO
   * @return {@link Result }
   * @description 处理工人申诉
   */
  @PostMapping("/workerclaim/solve")
  @ApiOperation("处理工人申诉")
  public Result solveWorkerClaim(@RequestBody ClaimSolveDTO claimSolveDTO) {
    log.info("处理工人申诉:{}", claimSolveDTO);
    adminService.solveWorkerClaim(claimSolveDTO);
    return Result.success();
  }

  /**
   * @param page
   * @param pageSize
   * @param claimStatus
   * @return {@link Result }<{@link PageResult }>
   * @description
   */
  @GetMapping("/workerclaim/page")
  @ApiOperation("分页查询工人申诉")
  public Result<PageResult> queryWorkerClaimWithStatus(Integer page, Integer pageSize,
      Integer claimStatus) {
    log.info("根据订单状态分页查询订单");
    PageResult p = adminService.queryWorkerClaimWithStatus(page, pageSize, claimStatus);
    return Result.success(p);
  }

  /**
   * @param claimSolveDTO
   * @return {@link Result }
   * @description 处理雇主申诉
   */
  @PostMapping("/employerclaim/solve")
  @ApiOperation("处理雇主申诉")
  public Result solveEmployerClaim(@RequestBody ClaimSolveDTO claimSolveDTO) {
    log.info("处理雇主申诉:{}", claimSolveDTO);
    adminService.solveEmployerClaim(claimSolveDTO);
    return Result.success();
  }

  /**
   * <<<<<<< HEAD 分页查询雇主申诉
   *
   * @param page
   * @param pageSize
   * @param claimStatus
   * @return {@link Result }<{@link PageResult }>
   * @description
   */
  @GetMapping("/employerclaim/page")
  @ApiOperation("分页查询雇主申诉")
  public Result<PageResult> queryEmployerClaimWithStatus(Integer page, Integer pageSize,
      Integer claimStatus) {
    log.info("根据申诉状态分页查询雇主申诉");
    PageResult p = adminService.queryEmployerClaimWithStatus(page, pageSize, claimStatus);
    return Result.success(p);
  }

  /**
   *查看申诉详情
   *
   * @param claimId
   * @return {@link Result }<{@link ClaimDetailVO }>
   * @description
   */
  @GetMapping("/claim/detail")
  @ApiOperation("查看申诉详情")
  public Result<AdminClaimDetailVO> getClaimDetail(String claimId) {
    log.info("申诉详情:{}", claimId);
    AdminClaimDetailVO adminClaimDetailVO = adminService.getClaimDetail(claimId);
    return Result.success(adminClaimDetailVO);
  }

  /**
   * 分页查询工人举报
   *
   * @param page
   * @param pageSize
   * @param complaintStatus
   * @return {@link Result }<{@link PageResult }>
   * @description
   */
  @GetMapping("/complaint/page")
  @ApiOperation("分页查询工人举报")
  public Result<PageResult> queryComplain(Integer page, Integer pageSize, Integer complaintStatus) {
    log.info("根据状态分页查询举报");
    PageResult p = adminService.queryComplaintWithStatus(page, pageSize, complaintStatus);
    return Result.success(p);
  }

  /**
   * 处 理工人举报
   *
   * @param complaintSolveDTO
   * @return {@link Result }
   * @description
   */
  @PostMapping("/complaint/solve")
  @ApiOperation("处理工人举报")
  public Result solveComplaint(@RequestBody ComplaintSolveDTO complaintSolveDTO) {
    log.info("处理工人举报:{}", complaintSolveDTO);
    adminService.solveComplaint(complaintSolveDTO);
    return Result.success();
  }

  /**
   * 查看举报详情
   * @param complaintId
   * @return {@link Result }<{@link ComplaintDetailVO }>
   * @description
   */
  @GetMapping("/complaint/detail")
  @ApiOperation("查看举报详情")
  public Result<AdminComplaintDetailVO> getComplaintDetail(String complaintId) {
    log.info("举报详情:{}", complaintId);
    AdminComplaintDetailVO adminComplaintDetailVO = adminService.getComplaintDetail(complaintId);
    return Result.success(adminComplaintDetailVO);
  }

  /**
   * * 分页查询雇主
   *
   * @param page
   * @param pageSize
   * @param userName
   * @return {@link Result }<{@link PageResult }>
   * @description
   */
  @GetMapping("/employer/page")
  @ApiOperation("分页查询雇主")
  public Result<PageResult> queryEmployer(Integer page, Integer pageSize, String userName) {
    log.info("查询雇主");
    PageResult p = adminService.queryEmployerWithName(page, pageSize, userName);
    return Result.success(p);
  }

  /**
   * 分页查询工人
   *
   * @param page
   * @param pageSize
   * @param userName
   * @return {@link Result }<{@link PageResult }>
   * @description
   */
  @GetMapping("/worker/page")
  @ApiOperation("分页查询工人")
  public Result<PageResult> queryWorker(Integer page, Integer pageSize, String userName) {
    log.info("查询工人");
    PageResult p = adminService.queryWorkerWithName(page, pageSize, userName);
    return Result.success(p);
  }

  /**
   * 设置雇主状态
   *
   * @param status
   * @param employerId
   * @return {@link Result }
   * @description
   */
  @GetMapping("/employer/status")
  @ApiOperation("设置雇主状态")
  public Result setEmployerStatus(Integer status, Long employerId) {
    log.info("设置雇主状态");
    adminService.setEmployerStatus(employerId, status);
    return Result.success();
  }

  /**
   *设置工人状态
   *
   * @param status
   * @param workerId
   * @return {@link Result }
   * @description
   */
  @GetMapping("/worker/status")
  @ApiOperation("设置工人状态")
  public Result setWorkerStatus(Integer status, Long workerId) {
    log.info("设置工人状态");
    adminService.setWorkerStatus(workerId, status);
    return Result.success();
  }
}
